<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('名称')" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          :placeholder="$t('名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('创建人')" prop="createBy">
        <UserSelect
          v-model="queryParams.createBy"
          :placeholder="this.$t('创建人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('最后更新人')" prop="updateBy">
        <UserSelect
          v-model="queryParams.updateBy"
          :placeholder="this.$t('最后更新人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:word:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row> -->

    <el-table v-loading="loading" :data="wordOnlineKeyWordList" border :default-expand-all="true" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">>
      <el-table-column :label="$t('名称')" prop="keyWord" show-overflow-tooltip min-width="200">
        <template slot-scope="scope">
          <i v-show="scope.row.type === 'folder'" class="el-icon-fold" style="margin: -1px 0px;width: 17px;height: 17px;" />
          <i v-show="scope.row.type === 'key_word_tree' || scope.row.type === 'key_word'" class="el-icon-text-png" style="margin: 0px 0px 4px;width: 15px;height: 15px;" />
          {{ scope.row.keyWord }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('标识')" prop="code" align="center" show-overflow-tooltip min-width="140" />
      <el-table-column :label="$t('类型')" align="center" prop="type" :formatter="formatTypeColumn" show-overflow-tooltip min-width="100" />
      <el-table-column :label="$t('引用情况')" align="center" prop="quoteNum" show-overflow-tooltip min-width="75">
        <template slot-scope="scope">
          <span v-if="scope.row.type ==='folder'" />
          <span v-else>
            <span v-if="scope.row.quoteNum !== 0" class="link-type" @click="viewQuoteList(scope.row, 'quote')">{{ scope.row.quoteNum }}</span>
            <span v-else>0</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('被引用情况')" align="center" prop="quotedNum" show-overflow-tooltip min-width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.type ==='folder'" />
          <span v-else>
            <span v-if="scope.row.quotedNum !== 0" class="link-type" @click="viewQuoteList(scope.row, 'quoted')">{{ scope.row.quotedNum }}</span>
            <span v-else>0</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('创建人')" align="center" prop="createBy.nickName" show-overflow-tooltip min-width="100" />
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" show-overflow-tooltip min-width="100" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('最后更新人')" align="center" prop="updateBy.nickName" show-overflow-tooltip min-width="100" />
      <el-table-column :label="$t('最后更新时间')" align="center" prop="updateTime" show-overflow-tooltip min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="80">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:word:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <!-- <el-button
            v-if="!isBuiltin"
            v-hasPermi="['system:word:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改关键字数据集对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" class="form-dialog" append-to-body>
      <!-- <wordOnlineKeyWordForm ref="wordOnlineKeyWordForm" /> -->
      <component :is="wordFormPath" v-if="formOpen" ref="wordForm" />
    </el-dialog>
    <!-- 添加或修改关键字数据集对话框 -->
    <el-dialog :title="$t(quoteTitle)" :visible.sync="quoteOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="" width="80%" append-to-body>
      <component :is="wordQuotePath" v-if="quoteOpen" ref="wordQuoteList" />
    </el-dialog>
  </div>
</template>

<script>
import { quoteList } from '@/api/system/word/keyword/wordOnlineKeyWord'
import wordOnlineKeyWordForm from './wordOnlineKeyWordForm.vue'
import { getAsyncComponent } from '@/utils'
export default {
  components: {
    wordOnlineKeyWordForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 关键字数据集表格数据
      wordOnlineKeyWordList: [],
      // 弹出层标题
      title: '关键字数据集',
      // 是否显示弹出层
      formOpen: false,
      quoteOpen: false,
      flag: 'quote',
      // 查询参数
      queryParams: {
        keyWord: undefined,
        templateType: undefined,
        sqlScript: undefined,
        createBy: undefined,
        updateBy: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 类型
      typeFolderOrKeyWOrdOptions: [],
      isBuiltinData: 'N',
      quoteTitle: '引用情况',
      data: {},
      type: 'quote',
      wordQuotePath: '',
      wordFormPath: ''
    }
  },
  computed: {
    isBuiltin() {
      // 是否内置的数据
      return this.isBuiltinData === 'Y'
    }
  },
  created() {
    this.$bus.$emit('async_done')
    // 类型
    this.getDicts('key_word_directory_text').then((response) => {
      this.typeFolderOrKeyWOrdOptions = response.data
    })
    // this.getList()
    // 获取是否内置数据的变量
    this.getIsBuiltinDataVariable()
  },
  mounted() {
    var path = '@/views/system/word/template/keywordDataSet/wordOnlineKeyWordQuoteList.vue'
    this.wordQuotePath = getAsyncComponent(path)
    var formPath = '@/views/system/word/template/keywordDataSet/wordOnlineKeyWordForm.vue'
    this.wordFormPath = getAsyncComponent(formPath)
  },
  methods: {
    init(row, type) {
      this.data = row
      this.type = type
      this.getList(row, type)
    },
    /** 查询关键字数据集列表 */
    getList(row, type) {
      this.loading = true
      const data = this.queryParams
      data.id = row.id
      quoteList(data, type).then(response => {
        this.wordOnlineKeyWordList = response.data
        this.loading = false
      })
    },
    formatTypeColumn(row) {
      return this.selectDictLabel(this.typeFolderOrKeyWOrdOptions, row.type)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        keyWord: undefined,
        busType: undefined,
        sqlScript: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList(this.data, this.type)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.wordOnlineKeyWordForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      // this.$nextTick(() => {
      //   // 调用form页面的 init 方法初始化数据
      //   this.$refs.wordOnlineKeyWordForm.init(row)
      // })
      this.$bus.$once('async_quote', () => {
        this.$nextTick(() => {
          this.$refs.wordForm && this.$refs.wordForm.init(row)
        })
      })
    },
    getIsBuiltinDataVariable() {
      this.getConfigKey('is_builtin_data').then(res => {
        if (res.msg && res.msg === 'Y') {
          this.isBuiltinData = res.msg
        }
      })
    },
    /**
     * 查看引用/被引用情况
     */
     viewQuoteList(row, type) {
      this.quoteOpen = true
      this.$bus.$once('async_done', () => {
        this.$nextTick(() => {
          this.$refs.wordQuoteList && this.$refs.wordQuoteList.init(row, type)
        })
      })
    }
  }
}
</script>
